Imports System.ComponentModel

Public Class MyCase
    Inherits BaseDescriptor

    Public Class CaseException
        Inherits Exception

        Public Sub New(ByVal message As String)
            MyBase.New(message)
        End Sub

    End Class

    Private WithEvents m_objMySections As New MySections
    Private WithEvents m_objMyOutputs As New MyOutputs(m_objMySections)
    Private m_blnOpening As Boolean
    Private m_blnSaved As Boolean = True
    Private m_strFile As String

    Public Event Building(ByVal msg As String)
    Public Event FileChanged()
    Public Event SectionChanged()

    <Browsable(False)> _
    Public ReadOnly Property File() As String
        Get
            Return m_strFile
        End Get
    End Property

    <CustomOrder(1), CustomDisplayName("Regions"), CustomDescription("Regions"), TypeConverter(GetType(CustomEditorConverter)), Editor(GetType(UIRegionsEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Public ReadOnly Property Regions() As MyRegions
        Get
            Return m_objMySections.Regions
        End Get
    End Property

    Public Sub Open(ByVal file As String)
        Dim strOutputDir As String = Me.OutputDir(file)

        m_blnOpening = True
        Try
            With My.Computer.FileSystem
                m_objMySections.Write(.ReadAllText(file).Replace(Environment.NewLine, vbLf).Replace(vbLf, Environment.NewLine))

                If .DirectoryExists(strOutputDir) Then Me.Outputs.Build(strOutputDir)
            End With
        Catch ex As MySections.SectionException
            Throw New CaseException(ex.Message)
        Finally
            m_blnOpening = False
            m_strFile = file
            RaiseEvent FileChanged()
        End Try
    End Sub

    <CustomOrder(3), CustomDisplayName("Outputs"), CustomDescription("Outputs"), TypeConverter(GetType(CustomEditorConverter)), Editor(GetType(UIOutputsEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Public ReadOnly Property Outputs() As MyOutputs
        Get
            Return m_objMyOutputs
        End Get
    End Property

    Public Sub Refresh()
        RaiseEvent SectionChanged()
    End Sub

    <Browsable(False)> _
    Public ReadOnly Property Sections() As MySections
        Get
            Return m_objMySections
        End Get
    End Property

    Public Overloads Sub Save()
        Me.Save(m_strFile)
    End Sub

    Public Overloads Sub Save(ByVal file As String)
        Dim strOutputDir As String = Me.OutputDir(file)

        m_strFile = Nothing
        Try
            With My.Computer.FileSystem
                If Me.Outputs.Updated Then
                    If .DirectoryExists(strOutputDir) Then
                        For Each strFile As String In .GetFiles(strOutputDir)
                            .DeleteFile(strFile)
                        Next
                    Else
                        .CreateDirectory(strOutputDir)
                    End If
                    Me.Outputs.Publish(strOutputDir)
                Else
                    If .DirectoryExists(strOutputDir) Then .DeleteDirectory(strOutputDir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                End If

                .WriteAllText(file, m_objMySections.Read(), False)
            End With
        Catch ex As Exception
            Throw New CaseException(ex.Message)
        Finally
            m_blnSaved = True
            m_strFile = file
            RaiseEvent FileChanged()
        End Try
    End Sub

    <Browsable(False)> _
    Public ReadOnly Property Saved() As Boolean
        Get
            Return m_blnSaved
        End Get
    End Property

    <CustomOrder(2), CustomDisplayName("Wavelengths"), CustomDescription("Wavelengths"), TypeConverter(GetType(CustomEditorConverter)), Editor(GetType(UIWavelengthsEditor), GetType(System.Drawing.Design.UITypeEditor))> _
    Public ReadOnly Property Wavelengths() As MyWavelengths
        Get
            Return m_objMySections.Wavelengths
        End Get
    End Property


    Private Sub m_objMySections_Changed() Handles m_objMySections.Changed
        If Not m_blnOpening AndAlso m_objMySections.Modified Then m_blnSaved = False
        RaiseEvent SectionChanged()
    End Sub

    Private Sub m_objMyOutputs_Status(ByVal msg As String) Handles m_objMyOutputs.Status
        RaiseEvent Building(msg)
    End Sub


    Private Function OutputDir(ByVal file As String) As String
        With My.Computer.FileSystem
            Dim strName As String = .GetName(file)
            Dim strBaseName As String = strName.Substring(0, strName.IndexOf("."))
            Dim strParentPath As String = .GetParentPath(file)

            Return .CombinePath(strParentPath, strBaseName)
        End With
    End Function

End Class
